//
//  subView2ForGetJobSheet.m
//  HondaPro_2
//
//  Created by Eric on 13-6-20.
//  Copyright (c) 2013年 glendale. All rights reserved.
//

#import "subView2ForGetJobSheet.h"
#import "UserDefinedCellForSubView2ForGetJobSheet.h"

@interface subView2ForGetJobSheet ()

/** 收费区分列表 */
@property(nonatomic,strong)NSMutableDictionary *chargeDiff;

//创建控件
-(void)createStaticLabels;
-(void)createTableView;



@end

@implementation subView2ForGetJobSheet
@synthesize nRowServiceTypeSel = _nRowServiceTypeSel;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        [self createStaticLabels];
        [self createTableView];
    }
    return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    
    [self freshenViewDis];
}

#pragma mark - 创建控件

-(void)createStaticLabels
{
    CGFloat w=self.bounds.size.width/8;
    CGRect frame = [self setupLabelWithTitle:@"收费区分" PreFrame:CGRectZero lableWidth:w];
    frame = [self setupLabelWithTitle:@"项目代码" PreFrame:frame lableWidth:w];
    frame = [self setupLabelWithTitle:@"项目名称" PreFrame:frame lableWidth:w];
    frame = [self setupLabelWithTitle:@"配件代码" PreFrame:frame lableWidth:w];
    frame = [self setupLabelWithTitle:@"备件名称" PreFrame:frame lableWidth:w];
    frame = [self setupLabelWithTitle:@"销售单价" PreFrame:frame lableWidth:w];
    frame = [self setupLabelWithTitle:@"数量" PreFrame:frame lableWidth:w];
    frame = [self setupLabelWithTitle:@"金额" PreFrame:frame lableWidth:w];
//    frame = [self setupLabelWithTitle:@"折扣" PreFrame:frame lableWidth:50];

}

-(CGRect)setupLabelWithTitle:(NSString *)title PreFrame:(CGRect)preFrame lableWidth:(CGFloat)lblW{
    CGFloat lblY = 0;
    CGFloat lblH = 50;

    CGFloat lblX = CGRectGetMaxX(preFrame);
    UILabel *lbl = [[UILabel alloc] init];
    lbl.text = title;
    lbl.frame = CGRectMake(lblX, lblY, lblW, lblH);
    lbl.backgroundColor = [UIColor clearColor];
    lbl.textColor = [UIColor grayColor];
    lbl.font = [UIFont fontWithName:@"ArialMT" size:16];
    lbl.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    lbl.numberOfLines = 2;
    lbl.textAlignment = NSTextAlignmentCenter;
    [self addSubview:lbl];
    return lbl.frame;
}

-(void)createTableView
{
    tableViewDis = [[UITableView alloc] initWithFrame:CGRectMake(0, 30+21, self.frame.size.width, 168-21)];
    tableViewDis.backgroundColor = [UIColor clearColor];
    
    tableViewDis.dataSource = self;
    tableViewDis.delegate = self;
    
    [self addSubview:tableViewDis];
}

#pragma mark - 刷新数据

-(void)freshenViewDis
{
    [tableViewDis reloadData];
}

#pragma mark - tableView dataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    
    NSInteger nRowSum = modelIns.partList.count;
    
    return 3+nRowSum;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    UserDefinedCellForSubView2ForGetJobSheet *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (nil == cell)
    {
        cell = [[UserDefinedCellForSubView2ForGetJobSheet alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    NSInteger row = indexPath.row;
    
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    NSArray *arrRepairItemList = modelIns.partList;
    cell.row = row;
    if (row<[arrRepairItemList count])
    {
        cell.data = arrRepairItemList[row];
    }
    else
    {
        cell.data = nil;
    }
    
    return cell;
}

//哪几行可以编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.canEdit) {
        MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
        
        if ([indexPath row]<modelIns.partList.count)
        {
            return YES;
        }
    }
    
    return NO;
}

#pragma mark - tableView delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = [indexPath row];
    
    //在此 将业务模型中对应的属性 设定为 当前选中的为第几行
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    
    if (row<modelIns.partList.count)
    {
       modelIns.nRowForPartList = row;
    }
    else
    {
        modelIns.nRowForPartList = -1;        
    }
}

#pragma mark - 出现删除按钮时，点击按钮时的操作

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //当前选中行进行设置
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    NSMutableDictionary *dic = modelIns.partList[indexPath.row];
    if (!dic[@"STATUS"]||[dic[@"STATUS"] isEqualToString:@"U"]) {//如果是存在数据库中的数据
        [dic setObject:@"D" forKey:@"STATUS"];
        [modelIns.deletedPartList addObject:dic];
    }
    [modelIns.partList removeObjectAtIndex:indexPath.row];
    [self freshenViewDis];
    [[NSNotificationCenter defaultCenter]postNotificationName:PART_LIST_MSG object:nil];
}


-(NSMutableDictionary *)chargeDiff{
    if (_chargeDiff == nil) {
        _chargeDiff = [NSMutableDictionary dictionary];
        NSMutableArray *arr = [self readershuDICimform][@"TM_CHARGE_PARTITION"];
        for (NSDictionary *dic in arr) {
            NSString *name = dic[@"NAME"];
            NSString *val = dic[@"CODE"];
            [_chargeDiff setObject:name forKey:val];
        }
    }
    return _chargeDiff;
}

-(NSMutableDictionary *)readershuDICimform
{
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path=[paths objectAtIndex:0];
    NSString *str=[path stringByAppendingPathComponent:@"Dic.text"];
    dic=[NSMutableDictionary dictionaryWithContentsOfFile:str];
    //nslog(@"%@",dic);
    return dic;
}

@end
